#include <iostream>
//#define for(i, N) for(int i=1; i<=N; ++i)
#define MAXN 54321
#define A -1
#define B -2
#define C -3
int s[]={C, A, B, C, A};
using namespace std;

int fa[MAXN]; int eat[MAXN], eaten[MAXN];

int find(int x){
    return x == fa[x] ? x : fa[x] = find(x);
}
int main() {
    fa[A] = A; fa[B] = B; fa[C] = C;
    int n, k;
    cin >> n >> k;
    const int N = n, K = k;
    for(int i=1; i<=N; i++){
        fa[i] = i;
    }
    int c, a, b, ans = 0;
    bool init = false;

    for(int i=1; i<=K; ++i){
        cin >> c >> a >> b;
        if(a > N || b > N) {++ans; continue;}
        if(c==2 && a == b) {++ans; continue;}

        int ga = find(a), gb = find(b);
        switch(c) {
            case 1:{
                if(ga<0 && gb<0){
                    if(ga != gb) ans++;
                }
                else if(ga<0 || gb<0){
                    if(ga<0) fa[gb] = ga;
                    else fa[ga] = gb;
                }
                else if(ga>0 && gb>0){

                }
                if(fa[a]<0 && fa[b]<0){
                    int s = 0; bool flg = false;
                    for(int j=a; eat[j]; ++j,++s){
                        if(eat[j] == b){
                            flg = true;
                            break;
                        }
                    }

                    if(flg){

                    }
                    else{
                        for(int neg=MAXN+1, k=b; eat[b]; --neg, ++k){
                            if(eat[k] == a){
                                flg = true;
                                break;
                            }
                        }
                    }

                    if(flg){

                    }
//                int pos=MAXN+1, neg = MAXN+1;
                    while(eat[a]) {++pos; if(eat[a]==b)break;}
                    while(eat[b]) {--neg; if(eat[b]==a)break;}
                }
                break;
            }
            case 2:{
                if(!init){
                    fa[a] = A; fa[b] = B;
                    init = true;
                }
                if(ga<0 && gb<0){
                    if(s[-ga+1] == s[-gb])break;
                    else ans++;

                }
                else if(ga<0 || gb<0){
                    if(ga<0) fa[gb] = s[-ga+1];
                    else fa[a] = s[-gb-1];
                }
                else if(ga>0 && gb>0){
                    eat[a] = b;
                    eaten[b] = a;
                }
                break;
            }
        }
    }
    cout << ans;

    return 0;
}
